/*
 * DepartmentEditServlet.java
 * Created on 25. juni 2008, 12:10
 */
package webshop.del;

import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import webshop.data.del.Department;
import webshop.data.del.Employee;



/**
 * DepartmentEditServlet
 * @author even
 */
public class DepartmentEditServlet extends AbstractDepartmentServlet {

    
    /**
     * Handle requests. Sends a html page containing the department list.
     *
     * @param req the request object
     * @param resp the response object
     *
     * @throws IOException
     * @throws ServletException
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        EntityManager em = emf.createEntityManager();
        PrintWriter out = response.getWriter();
        
        String tmp;
        Department dep = null;
        tmp = request.getParameter("depno");
        if (tmp != null) {
            long depNo = Long.valueOf(tmp);
            dep = em.find(Department.class, depNo);
        }
        
        response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache");
        response.setHeader("Expires", "0");

        out.println("<html><head><title>"+this.getClass().getName()+"</title></head><body>");
        if (dep != null) {
            out.println("<h1>Edit Department</h1>");
            out.println("<form action=\"depsave\">");
            out.println("<input type=\"hidden\" name=\"depno\" value=\""+dep.getDepNo()+"\"/>");
            out.println("<table>");
            printField(out, "Name", "name", dep.getName());
            printDatum(out, "Dep.no.", dep.getDepNo());
            out.println("</table>");
            out.println("<input type=\"submit\"/>");
            out.println("</form>");
            out.println("<h3>Employees</h3>");
            out.println("<table>");
            for (Employee emp : dep.getEmployees()) {
                out.println("<tr><td><a href=\"empedit?empno="+emp.getEmpNo()+"\">"+emp.getName()+"</a></td></tr>");
            }
            out.println("</table>");
            out.println("<a href=\"empedit?depno="+dep.getDepNo()+"\">Add Employee</a>");
            
        }
        else {
            out.println("<h1>New Department</h1>");
            out.println("<form action=\"depsave\">");
            out.println("<table>");
            printField(out, "Name", "name", "");
            out.println("</table>");
            out.println("<input type=\"submit\"/>");
            out.println("</form>");
        }

        out.println("<hr/>");
        out.println("<p><a href=\"index.html\">Home</a> &gt; <a href=\"deplist\">Department List</a></p>");
        out.println("</body>");
        out.println("</html>");
        
        em.close();
    }
}
